<?php
if (!(defined('IN_IA')))
{
    exit('Access Denied');
}
/**
 * User: 邓志锋 <280160522@qq.com> <http://www.diygw.com>
 * Date: 2018-07-28
 * Time: 下午 6:06
 */
class Address_Diygwcom extends Interceptor_Diygwcom
{
    public function index()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $extendValues = $this->getDashboardExtends($dashboardid);
        include $this->template();
    }


    public function data()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $uid =$this->getUid();
        $list = pdo_getall(getTableName("ADDRESS"),array('dashboard_id'=>$dashboardid,'user_id'=>$uid));
        $is_def = $_GPC["isdef"];

        $data=[];
        if($is_def=='1'){
            foreach ($list as $key=>$item){
                if($item['is_def']==1){
                    $data[]=$item;
                    break;
                }
            }
            if(count($data)==0){
                $data[]=$list[0];
            }
            $list=$data;
        }
        echo json_encode(["status"=>"success","message"=>"获取数据成功","rows"=>$list,"total"=>count($list),"totalPage"=>1]);
    }


    public function edit()
    {
        global $_GPC, $_W;
        $id=$_GPC['id'];
        if(!empty($id)){
            $address = pdo_get(getTableName("ADDRESS"),array("id"=>$id));
            if($address['user_id']!=$this->getUid()){
                return $this->getErrorPage("没有权限修改地址");
            }
        }else{
            $address=[];
        }
        $dashboardid = $_GPC["dashboardid"];
        $extendValues = $this->getDashboardExtends($dashboardid);
        include $this->template();
    }

    public function save()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $uid =$this->getUid();
        $is_def = safe_gpc_string($_GPC["is_def"]);

        $id=$_GPC['id'];
        if(!empty($id)){
            $address = pdo_get(getTableName("ADDRESS"),array("id"=>$id));
            if($address['user_id']!=$this->getUid()){
                return $this->getErrorMessage("没有权限修改地址");
            }
        }
        if($is_def=="1"){
            pdo_update(getTableName("ADDRESS"),array("is_def"=>0),array("user_id"=>$uid,"dashboard_id"=>$dashboardid));
        }

        $address['dashboard_id']=$dashboardid;
        $address['user_id']=$uid;
        $address['name']=safe_gpc_string($_GPC["name"]);
        $address['tel']=safe_gpc_string($_GPC["tel"]);
        $address['address']=safe_gpc_string($_GPC["address"]);
        $address['create_time']=date("Y-m-d H:i:s", time());
        $address['update_time']=date("Y-m-d H:i:s", time());
        $address['is_def']=$is_def;
        if(!empty($id)){
            $result = pdo_update(getTableName("ADDRESS"),$address,array("id"=>$id));
        }else{
            $result = pdo_insert(getTableName("ADDRESS"),$address);
        }
        if(false!==$result){
            return $this->getSuccessMessage("保存地址成功");
        }else{
            return $this->getSuccessMessage("保存地址失败");
        }
    }


    public function delete()
    {
        global $_GPC, $_W;
        $ids    =   safe_gpc_array($_GPC['values']);
        foreach ($ids as $id){
            $address = pdo_get(getTableName("ADDRESS"),array("id"=>$id));
            if($address['user_id']!=$this->getUid()){
                return $this->getErrorMessage("没有权限修改地址");
            }
        }
        $result = pdo_delete(getTableName("ADDRESS"),array("id"=>$ids));
        if(false!==$result){
            return $this->getSuccessMessage("删除地址成功");
        }else{
            return $this->getSuccessMessage("删除地址失败");
        }
    }

    public function changeDef()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $uid =$this->getUid();


        $id=$_GPC['id'];
        if(!empty($id)){
            $address = pdo_get(getTableName("ADDRESS"),array("id"=>$id));
            if($address['user_id']!=$this->getUid()){
                return $this->getErrorMessage("没有权限修改地址");
            }
        }

        pdo_update(getTableName("ADDRESS"),array("is_def"=>0),array("user_id"=>$uid,"dashboard_id"=>$dashboardid));

        $result = pdo_update(getTableName("ADDRESS"),array("is_def"=>1),array("id"=>$id));
        if(false!==$result){
            return $this->getSuccessMessage("设置成功");
        }else{
            return $this->getSuccessMessage("设置失败");
        }
    }
}